#include "channel.h"

struct channel *channel_new(int fd, int events,
                            event_read_callback read_callback, event_write_callback write_callback,
                            void *data)
{
    struct channel *chan = malloc(sizeof(struct channel));
    chan->fd = fd;
    chan->events = events;
    chan->read_callback = read_callback;
    chan->write_callback = write_callback;
    chan->data = data;
    return chan;
}

int channel_write_event_is_enabled(struct channel *chan)
{
    return chan->events & EVENT_WRITE;
}

int channel_write_event_enable(struct channel *chan)
{
    struct event_loop *eloop = (struct event_loop *)chan->data;
    chan->events = chan->events | EVENT_WRITE;
    // 更新 channel 的监听事件
    event_loop_update_channel_event(eloop, chan->fd, chan);
}

int channel_write_event_disable(struct channel *chan)
{
    struct event_loop *eloop = (struct event_loop *)chan->data;
    chan->events = chan->events & ~EVENT_WRITE; // 禁止可写
    // 更新 channel 的监听事件
    event_loop_update_channel_event(eloop, chan->fd, chan);
}
